// packageH/Advertising/advertisingAdvertisers/advertisingAdvertisers.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statistical: {},
    popover_text: "全部",
    actions: [{
      text: "全部"
    }],
    info: {},
    status: 0, //状态
    activeName: 0,
    dataList: [],
    titleArr: [{
      name: "广告审核",
      code: "audit",
      isPopover: false
    }],
    isLoadMore: true,
    showTabs: false,
    isShowPopover: false,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    this.getData();
  },

  init() {
    let dataList = [{
      isLoadMore: true,
      page: 1,
      total_page: 0,
      url: "plugin.ad-serving.frontend.advertisers.index.advertisingCheck",
      list: []
    }];
    this.setData({
      showTabs: false,
      activeName: 0,
      dataList: dataList,
      isLoadMore: true
    });
    this.handleClick();
  },
  getData() {
    let urlStr = "";
    urlStr += app.getNetAddresss("plugin.ad-serving.frontend.advertisers.index.memberInfo");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let plugins_status = res.data.plugins_status;
          let titleArr = this.data.titleArr;
          let dataList = this.data.dataList;
          if (plugins_status.video_share) {
            //视频分享插件开启
            titleArr.push({
              name: "视频订单",
              code: "video",
              isPopover: false
            });
            dataList.push({
              isLoadMore: true,
              page: 1,
              total_page: 0,
              list: [],
              url: "plugin.ad-serving.frontend.advertisers.index.getOrderInfo"
            });
          }
          if (plugins_status.room) {
            //直播插件开启
            titleArr.push({
              name: "直播订单",
              code: "live",
              isPopover: false
            });
            dataList.push({
              isLoadMore: true,
              page: 1,
              total_page: 0,
              list: [],
              url: "plugin.ad-serving.frontend.advertisers.index.getOrderInfo"
            });
          }
          this.setData({
            info: res.data.info,
            titleArr: titleArr,
            dataList: dataList,
            showTabs: true
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onClick(e) {
    this.setData({
      popover_text: '全部',
      activeName: e.detail.index
    });
    this.handleClick();
  },
  handleClick(_status) {
    this.changeActions();
    let Json = {};
    console.log("1111111111111111111111", _status);
    if (_status) {
      Json.status = Number(_status);
      this.status = _status;
    } else {
      this.status = 0;
    }
    if (this.data.titleArr[this.data.activeName].code == "video") {
      Json.put_in_type = 2; //1-直播间，2-短视频，不传默认直播间
    }
    let urlStr = app.getNetAddresss(this.data.dataList[this.data.activeName].url);
    app._getNetWork({
      url: urlStr,
      data: Json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            statistical: res.data.statistical ? res.data.statistical : {},
            ['dataList[' + this.data.activeName + '].isLoadMore']: true,
            ['dataList[' + this.data.activeName + '].total_page']: res.data.list.last_page,
            ['dataList[' + this.data.activeName + '].list']: res.data.list.data
          });

        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  changePopover(){
    this.setData({
      isShowPopover: !this.data.isShowPopover
    });
  },
  clickPopover(e){
    this.handleClick(e.currentTarget.dataset.id);
    this.changePopover();
    this.setData({
      popover_text: e.currentTarget.dataset.name
    });
  },
  changeActions() {
    let actions = [];
    if (this.data.activeName == 0) {
      actions = [{
        text: "全部",
        status: ""
      },
      {
        text: "待审核",
        status: "0"
      },
      {
        text: "通过",
        status: "1"
      },
      {
        text: "驳回",
        status: "-1"
      }
      ];
    } else {
      actions = [{
        text: "全部",
        status: ""
      },
      {
        text: "投放中",
        status: "1"
      },
      {
        text: "下架",
        status: "0"
      }
      ];
    }
    this.setData({
      actions: actions
    });
  },
  gotoUrl(e){
    let detail = e.currentTarget.dataset;
    if(detail.tag == 'advertisingRelease'){
      wx.navigateTo({url: '/packageH/Advertising/advertisingRelease/advertisingRelease' });
    }else if(detail.tag == 'advertisingAdDetails'){
      wx.navigateTo({url: '/packageH/Advertising/advertisingAdDetails/advertisingAdDetails?id='+ detail.id });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.dataList[this.data.activeName].isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  _getMoreData() {
    let that = this;
    this.setData({
      ['dataList[' + that.data.activeName + '].isLoadMore']: false
    });
    if (this.data.dataList[this.data.activeName].page >= this.data.dataList[this.data.activeName].total_page) {
      return;
    } else {
      let Json = {};
      Json.status = Number(this.status);
      if (this.data.titleArr[this.data.activeName].code == "video") {
        Json.put_in_type = 2; //1-直播间，2-短视频，不传默认直播间
      }
      this.data.dataList[this.data.activeName].page = this.data.dataList[this.data.activeName].page + 1;
      let urlStr = app.getNetAddresss(this.data.dataList[this.data.activeName].url);
      urlStr += "&page=" + this.data.dataList[this.data.activeName].page;

      app._getNetWork({
        url: urlStr,
        data: Json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.list;
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: true,
              ['dataList[' + that.data.activeName + '].list']: that.data.dataList[that.data.activeName].list.concat(myData.data)
            });
          } else {
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: false,
              ['dataList[' + that.data.activeName + '].page']: that.data.dataList[that.data.activeName].page - 1
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});